#include "FileReader.h"

vector<Object*> readFileforObjects (char * fname)
{
	unsigned int i;
	vector<Object*> fileObjects(0);
	ifstream fin;
	fin.open(fname);
	//cout << "Contents of file: \n";
	string str;
	vector<string> strList(2);
	while (!fin.eof())
	{
		std::getline(fin, str);
		strList = splitString(str);
		if (strList.size() == 0)
			continue;
		vector<string> objDesc(0);
        if (strList[0].compare("NEWOBJECT") == 0)
		{
			if (strList[1].compare("square") == 0)
			{
				while (!fin.eof())
				{
					std::getline(fin, str);
					strList = splitString(str);
					if (strList.size() == 0)
						continue;
					if (strList[0].compare("ENDOBJECT") == 0)
						break;
					for (i = 0; i < strList.size(); i++)
						objDesc.push_back(strList.at(i));
				}
                //Object * obj = (Object *) malloc(sizeof(Object));
                //PlaneObject temp = ;
				fileObjects.push_back( parseSquare(objDesc) );
			} else if (strList[1].compare("sphere") == 0)
			{
				while (!fin.eof())
				{
					std::getline(fin, str);
					strList = splitString(str);
					if (strList.size() == 0)
						continue;
					if (strList[0].compare("ENDOBJECT") == 0)
						break;
					for (i = 0; i < strList.size(); i++)
						objDesc.push_back(strList.at(i));
				}
                //SphereObject * obj = (SphereObject *) malloc(sizeof(SphereObject));
                //*obj = parseSphere(objDesc);
				fileObjects.push_back(parseSphere(objDesc));

			} else if (strList[1].compare("pillar") == 0)
			{

			}
		}
	}
	fin.close();
	return fileObjects;
}

vector<LightPoint> readFileforLights (char * fname)
{
	unsigned int i;
	vector<LightPoint> fileObjects(0);
	ifstream fin;
	fin.open(fname);
	string str;
	vector<string> strList(2);
	while (!fin.eof())
	{
		std::getline(fin, str);
		strList = splitString(str);
		if (strList.size() == 0)
			continue;
		vector<string> objDesc(0);
		if (strList[0].compare("NEWOBJECT") == 0)
		{
			if (strList[1].compare("light") == 0)
			{
				while (!fin.eof())
				{
					std::getline(fin, str);
					strList = splitString(str);
					if (strList.size() == 0)
						continue;
					if (strList[0].compare("ENDOBJECT") == 0)
						break;
					for (i = 0; i < strList.size(); i++)
						objDesc.push_back(strList.at(i));
				}
				fileObjects.push_back(parseLight(objDesc));
			}
		}
	}
	fin.close();
	return fileObjects;
}

vector<string> splitString(string str)
{
	vector<string> tokens;
    istringstream iss(str);
	
	copy(istream_iterator<string>(iss),
         istream_iterator<string>(),
         back_inserter<vector<string> >(tokens));

	return tokens;
}

int countWords(string str)
{
	int count = 1;
	for (int i = 0; i < str.length(); i++)
	{
		if (str[i] == ' ')
			count++;
	}
	return count;
}

PlaneObject * parseSquare(vector<string> attr)
{
	Point3 center;
	Color4 color(.8, .8, .8, 1);
	double width=1, height=1, depth=1, reflectance=.1;
	double xRot=0, yRot=0, zRot=0;
	int i;
	for (i = 0; i < attr.size(); i++)
	{
		if (attr.at(i).compare("CENTER") == 0)
		{
			center.x = atof(attr.at(i+1).c_str());
			center.y = atof(attr.at(i+2).c_str());
			center.z = atof(attr.at(i+3).c_str());
			i+=3;
		} else if (attr.at(i).compare("DIMENSIONS") == 0)
		{
			width = atof(attr.at(i+1).c_str());
			height = atof(attr.at(i+2).c_str());
			depth = atof(attr.at(i+3).c_str());
			i+=3;
		} else if (attr.at(i).compare("COLOR") == 0)
		{
			color.red = atof(attr.at(i+1).c_str());
			color.green = atof(attr.at(i+2).c_str());
			color.blue = atof(attr.at(i+3).c_str());
			i+=3;
		} else if (attr.at(i).compare("REFLECTANCE") == 0)
		{
			reflectance = atof(attr.at(i+1).c_str());
			i++;
		} else if (attr.at(i).compare("ROTATION") == 0)
		{
			xRot = atof(attr.at(i+1).c_str());
			yRot = atof(attr.at(i+2).c_str());
			zRot = atof(attr.at(i+3).c_str());
			i++;
		}
	}
	if (xRot == 0 && yRot == 0 && zRot == 0)
    {
        return createSquare(center, width, height, depth, color, reflectance);
    }
    return createSquare(center, width, height, depth, color, reflectance, xRot, yRot, zRot);
}

SphereObject * parseSphere(vector<string> attr)
{
	Point3 center;
	Color4 color(.8, .8, .8, 1);
	double radius=1, reflectance=.1;
	int i;
	for (i = 0; i < attr.size(); i++)
	{
		if (attr.at(i).compare("CENTER") == 0)
		{
			center.x = atof(attr.at(i+1).c_str());
			center.y = atof(attr.at(i+2).c_str());
			center.z = atof(attr.at(i+3).c_str());
			i+=3;
		} else if (attr.at(i).compare("RADIUS") == 0)
		{
			radius = atof(attr.at(i+1).c_str());
			i++;
		} else if (attr.at(i).compare("COLOR") == 0)
		{
			color.red = atof(attr.at(i+1).c_str());
			color.green = atof(attr.at(i+2).c_str());
			color.blue = atof(attr.at(i+3).c_str());
			i+=3;
		} else if (attr.at(i).compare("REFLECTANCE") == 0)
		{
			reflectance = atof(attr.at(i+1).c_str());
			i++;
		}
	}
	//cout << radius << endl;
	return createSphere(center, radius, color, reflectance);
}

LightPoint parseLight(vector<string> attr)
{
	Point3 center;
	Color4 color(0.8, 0.8, 0.8, 1);
	double intensity = 1;
	int i;
	for (i = 0; i < attr.size(); i++)
	{
		if (attr.at(i).compare("CENTER") == 0)
		{
			center.x = atof(attr.at(i+1).c_str());
			center.y = atof(attr.at(i+2).c_str());
			center.z = atof(attr.at(i+3).c_str());
			i+=3;
		} else if (attr.at(i).compare("INTENSITY") == 0)
		{
			intensity = atof(attr.at(i+1).c_str());
			i++;
		} else if (attr.at(i).compare("COLOR") == 0)
		{
			color.red = atof(attr.at(i+1).c_str());
			color.green = atof(attr.at(i+2).c_str());
			color.blue = atof(attr.at(i+3).c_str());
			i+=3;
		}
	}
	return LightPoint(center, color, intensity);
}